package com.hzcx.app.simplechat.ui.setting.adapter;

import android.media.MediaPlayer;
import android.view.View;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.setting.bean.GroupMessageListBean;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.util.mediaplayer.ManagedMediaPlayer;

import java.io.IOException;
import java.util.List;

public class GroupMessageListAdapter extends BaseQuickAdapter<GroupMessageListBean, BaseViewHolder> {

    private static final int TYPE_DEFAULT = 0;
    private static final int TYPE_TEXT = 1;
    private static final int TYPE_VOICE = 2;
    private static final int TYPE_IMAGE = 3;

    private static final int DEFAULT = R.layout.rv_item_group_message_list_text;
    private static final int TEXT = R.layout.rv_item_group_message_list_text;
    private static final int VOICE = R.layout.rv_item_group_message_list_voice;
    private static final int IMAGE = R.layout.rv_item_group_message_list_image;

    public GroupMessageListAdapter(@Nullable List<GroupMessageListBean> data) {
        super(data);
        setMultiTypeDelegate(new MultiTypeDelegate<GroupMessageListBean>() {
            @Override
            protected int getItemType(GroupMessageListBean groupMessageListBean) {
                if (groupMessageListBean.getChat_type() == TYPE_TEXT) {
                    return TYPE_TEXT;
                } else if (groupMessageListBean.getChat_type() == TYPE_VOICE) {
                    return TYPE_VOICE;
                } else if (groupMessageListBean.getChat_type() == TYPE_IMAGE) {
                    return TYPE_IMAGE;
                } else {
                    return TYPE_DEFAULT;
                }
            }
        });

        getMultiTypeDelegate().registerItemType(TYPE_TEXT, TEXT)
                .registerItemType(TYPE_VOICE, VOICE)
                .registerItemType(TYPE_IMAGE, IMAGE)
                .registerItemType(TYPE_DEFAULT, DEFAULT);
    }

    @Override
    protected void convert(BaseViewHolder helper, GroupMessageListBean item) {

        helper.setText(R.id.tv_date, item.getCreatetime());
        helper.setText(R.id.tv_num, item.getMember_count() + "位收信人");
        helper.setText(R.id.tv_nick_name_group, item.getChat_member_ids());

        if (item.getChat_type() == TYPE_TEXT) {
            helper.setText(R.id.tv_content, item.getChat_content());
        } else if (item.getChat_type() == TYPE_IMAGE) {
            GlideUtils.loadImgAsCenter(mContext, item.getChat_content(), helper.getView(R.id.iv_img));
        } else if (item.getChat_type() == TYPE_VOICE) {
            helper.setText(R.id.tv_content, item.getAudio_time() + "");
            ManagedMediaPlayer mediaPlayer = new ManagedMediaPlayer();
            try {
                mediaPlayer.setDataSource(item.getChat_content());
                mediaPlayer.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
            helper.setOnClickListener(R.id.tv_content, view -> {
                if (mediaPlayer.getState() == ManagedMediaPlayer.Status.STARTED) {
                    mediaPlayer.pause();
                } else {
                    mediaPlayer.start();
                }
            });
        } else {
            helper.setText(R.id.tv_content, item.getChat_content());
        }

        helper.addOnClickListener(R.id.tv_resend);

    }
}
